# -*- coding: utf-8 -*-
# @File    : implementSqlOA.py
# @Date    : 2021-08-27
# @Author  : Richard

import os
import configparser
import time
import dmPython
import xlrd
from xlrd import xldate_as_tuple
import logging
import pandas
import re


def main():
    ip_list = []
    dir_sec = "dir"
    db_sec = "database"
    ini_file = ""

    # 数据库及文件目录信息
    read_ini = Read_ini()
    dir_data = read_ini.values_by_key(dir_sec)
    db_data = read_ini.values_by_key(db_sec)
    print("dir_data:{0}".format(dir_data))
    print("db_data:{0}".format(db_data))

    # 获取服务器IP
    get_excel_data = ExcelData(data_path=dir_data[0], sheetname=db_data[3])
    server_data = get_excel_data.readExcel()
    # print("server_data:{0}".format(server_data))

    # # # 获取SQL文件
    resolusql = ResoluSql(dir_data[1])
    sql_file = resolusql.get_sql_file()

    for itmes in server_data:
        ip = itmes[db_data[2]]
        pas_id = itmes[db_data[4]]
        meting_api = itmes[db_data[5]]
        print("开始[{0}]执行".format(ip))

        # print("IP:{0}".format(ip))
        # print("pas_id:{0}".format(pas_id))
        # print("meting_api:{0}".format(meting_api))

        # 遍历SQL文件
        for sf in sql_file:
            # print("sf:{0}".format(sf))
            sqls = resolusql.read(sf)
            # 需要替换一些值的
            # sqls = str(sqls).replace("#passid#",str(pas_id)).replace("#meting_api#",str(meting_api))
            # print("sqls:{0}".format(sqls))
            # time.sleep(1000)
            db = DB(sql=sqls, ip=ip, user=db_data[0], port=db_data[1])
            db.run_sql()
        print("[{0}]执行成功".format(ip))


if __name__ == '__main__':
    main()
